<?php
/**
 * 娱乐管理
 * User: admin
 * Date: 2017/12/12
 * Time: 11:06
 */

namespace Api\Controller;
use \Think\Upload\Driver\Qiniu\QiNiuStorage;


class VideoController extends ApiController
{

    /**
     * 轮播图
     */
    public  function  carousel(){
        $Carousel=M('carousel','cmf_');
        $Video =M('video','cmf_');
        $type = I('type');
        $map['type'] = $type;
        $list = $Carousel->where($map)->order('uptime desc')->select();
        if($type == 3){
            foreach ($list as $li){
                $li['url'] = M('game_recommend','cmf_')->where('id='.$li['vid'])->getField('url');
            }
        }else{
            foreach ($list as &$li){
                $li['is_charge']= $Video->where('id='.$li['vid'])->getField('is_charge');
            }
        }
        response(1, $list);
    }

    /**
     * 搜索
     */
    public function search(){
        $type=I('type');
        $key = I('name');
        if($type == 3){
            $Game = M('game_recommend','cmf_');
            $map['name'] = array('like','%'.$key.'%');
            $list = $Game->where($map)->order('uptime desc')->select();
        }else{
            $Video = M('video','cmf_');
            $map['type'] = $type;
            $map['name'] =  array('like','%'.$key.'%');
            $list = $Video->where($map)->order('uptime desc')->select();
        }
        response(1,$list);
    }

    /**
     * 获取七牛token
     */
     public function  uptoken(){
         $config = array(
             'accessKey'=>C('ACCESSKEY'),
             'secrectKey'=>C('SECRECTKEY'),
             'bucket'=>C('BUCKET'),
             'domain'=>C('DOMAIN')
         );
         $qiniu = new  \Think\QiniuStorage($config);
         $token = $qiniu->getToken($config);
         $data['qiniu_token'] = $token;
         response(1,$data);
     }

    /**
     * 上传视频
     */
    public function addVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');

        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $data['type'] =2;
        $data['video_path'] = C('DOMAIN').I('qiniu_key');
        $data['thumb2']     = C('DOMAIN').I('img_key');
        $data['name']       = I('name');
        $data['charge_money'] = I('charge_money');
        if(I('charge_money')) {
            $data['is_charge'] = 1;
        }
        if(I('down_money')) {
            $data['down_money'] = I('down_money');
            $data['is_down']  =1;
        }
        $data['qiniu_key']    = I('qiniu_key');
        $data['uid']           = $uid;
        $data['detail']        = I('detail');
        $data['uptime']        = time();
        $result = $Video->add($data);
        if($result){
            $data['id'] = $result;
            response(1,$data,'上传成功');
        }else{
            response(-1,null,'上传失败');
        }

    }

    /**
     * 我的视频
     */
    public  function  myVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');

        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');
        $list = $Video->where('uid='.$uid)->order('uptime desc')->select();
        foreach ($list as &$li){
            $li['uptime'] =date('Y-m-d H:i:s',$li['uptime']);
        }

        response(1,$list);
    }

    /**
     * 删除视频
     */
    public  function  delVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');
        $Recommend_video = M('recommend_video','cmf_');
        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $ids = I('id');

        $where['uid'] =$uid;
        $where['id'] = array('in',$ids);
        $result= $Video->where($where)->delete();
        $map['vid']=array('in',$ids);
        $Recommend_video->where($map)->delete();

        if($result){
            response(1,null,'删除成功');
        }else{
            response(-1,null,'删除失败');
        }
    }


    /**
     * 影视首页推荐
     */
    public function  rec_videoList(){
        $Top_rec = M('top_rec','cmf_');
        $Video = M('video','cmf_');
        $Teleplay = M('teleplay','cmf_');
        $video_list =$Top_rec->where('type=1')->order('uptime desc')->select();
        foreach ($video_list as &$li){
            $li['name'] = $Video->where('id='.$li['vid'])->getField('name');
            $li['video_type'] = $Video->where('id='.$li['vid'])->getField('video_type');
            $li['is_charge'] = $Video->where('id='.$li['vid'])->getField('is_charge');
        }
        $data['video_list'] = $video_list;
        $teleplay_list = $Top_rec->where('type=2')->order('uptime desc')->select();
        foreach ($teleplay_list as &$tli){
            $tli['name'] = $Video->where('id='.$tli['vid'])->getField('name');
            $tli['video_type'] = $Video->where('id='.$tli['vid'])->getField('video_type');
            $count=$Teleplay->where('is_charge=1 and video_id='.$tli['vid'])->count();

            if($count>0){
                $tli['is_charge']='1';
            }else{
                $tli['is_charge']='0';
            }
        }
        $data['teleplay_list']  = $teleplay_list;
        response(1, $data);
    }
    /**
     * 影视电影和电视剧列表
     */
    public function videoList(){
        $type = I('type');
        $Video = M('video','cmf_');
        $Teleplay = M('teleplay','cmf_');
        $map['video_type'] =$type;
        $map['type'] =1;
        $video_list = $Video->where($map)->order('uptime desc')->select();

        if($type ==2){
            foreach ($video_list as &$li){
                $li['jishu'] =$Teleplay->where('video_id='.$li['id'])->order('jishu desc')->getField('jishu');
                $where['video_id'] = $li['id'];
                $where['is_charge'] = 1;
                $is_charge = $Teleplay->where($where)->count();
                if($is_charge>0){
                    $li['is_charge'] = '1';
                }else{
                    $li['is_charge'] = '0';
                }

            }
        }
        $data['video'] = $video_list;
        response(1, $data);
    }

    /**
     * 视频首页推荐
     */
    public function rec_video(){
        $Top_rec = M('top_rec','cmf_');
        $Video = M('video','cmf_');
        $video_list =$Top_rec->where('type=3')->order('uptime desc')->select();
        foreach ($video_list as &$li){
            $li['name'] = $Video->where('id='.$li['vid'])->getField('name');
            $li['video_type'] = $Video->where('id='.$li['vid'])->getField('video_type');
            $li['is_charge'] = $Video->where('id='.$li['vid'])->getField('is_charge');
        }
        $data['video_list'] = $video_list;

        response(1, $data);
    }

    /**
     * 全部视频列表
     */
    public  function   all_video(){
        $Video = M('video','cmf_');
        $map['type'] =2;
        $video_list = $Video->where($map)->order('uptime desc')->select();
        $data['video'] = $video_list;
        response(1, $data);
    }


    /**
     * 视频详情
     */
    public function videoDetails(){
        only_login();

        $uid=API_UID;
        $id = I('id');
        $Video = M('video','cmf_');
        $Thumbsup =M('thumbs_up','cmf_');
        $Comment = M('comment','cmf_');
        $Users = M('users','cmf_');
        $Releplay=M('teleplay','cmf_');
        $CollectVideo =M('collect_video','cmf_');
        $Purchase  = M('purchase','cmf_');
        $Download_video = M('download_video','cmf_');
        $map['id'] =$id;
        $video = $Video->where($map)->find();

        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();
        if($video['charge_money']) {
            $video['charge_money'] = $video['charge_money'] / $config['cash_rate'];

        }
        if($video['down_money']) {
            $video['down_money'] = $video['down_money'] / $config['cash_rate'];
        }
        if($video['uid'] == '1') {
            $video['nickname'] = "平台上传";
        }else{
            $nickname = $Users->where('id=' . $video['uid'])->getField('user_nicename');
            if($nickname){
                $video['nickname'] = $nickname;
            }else{
                $video['nickname'] = '';
            }
        }
        //播放数增加
        $Video->where('id='.$id)->setInc('times',1);
        //点赞量
        $map_ups['video_id'] = $id;
        $thumbs = $Thumbsup->where($map_ups)->count();
        $video['thumbs_up_nums'] = $thumbs;
        if($video['video_type'] == 1){
            $video['video_path']= $video['video_path'];
            $video['thumb2'] = $video['thumb2'];
        }else if ($video['video_type'] == 2){
            $jishu=I('jishu');
            if(!$jishu){
                $jishu=1;
            }
            $maps['video_id'] = $id;
            $jishus=$Releplay->where($maps)->field('jishu')->order('jishu')->select();

            foreach ($jishus as $key=>$ji){
                $video['jishu'][$key]['ji'] = $ji['jishu'];
                $map_tv['jishu'] = $ji['jishu'];
                $map_tv['video_id'] = $id;
                $teleplay = $Releplay->where($map_tv)->find();
                $video['jishu'][$key]['is_charge']= $teleplay['is_charge'];
                $video['jishu'][$key]['charge_money'] =$teleplay['charge_money'];
                $video['jishu'][$key]['is_down']= $teleplay['is_down'];
                $video['jishu'][$key]['down_money'] = $teleplay['down_money'];
                $video_path =  $teleplay['video_path'];
                $video['jishu'][$key]['path'] = $video_path;
                if($uid){
                    //是否购买
                    $map_pur['uid'] = $uid;
                    $map_pur['jishu'] = $ji['jishu'];
                    $map_pur['vid'] = $video['id'];
                    $map_pur['status'] = 1;
                    $map_pur['type'] = 1;
                    $is_purchase = $Purchase->where($map_pur)->find();
                    $video['jishu'][$key]['is_purchase']=$is_purchase?1:0;

                    //是否下载
                    if($video['uid'] == $uid){
                        $is_download = 1;
                    }else {
                        $map_down['uid'] = $uid;
                        $map_down['jishu'] = $ji['jishu'];
                        $map_down['vid'] = $video['id'];
                        $map_down['status'] = 1;
                        $map_down['type'] = 2;
                        $is_download = $Purchase->where($map_down)->find();
                        $video['jishu'][$key]['is_download'] = $is_download?1:0;
                    }
                }else{
                    $video['jishu'][$key]['is_purchase']=0;
                    $video['jishu'][$key]['is_download'] =0;
                }
            }

            $maps['jishu'] = $jishu;
            $teleplay = $Releplay->where($maps)->find();

            
            $video['thumb2'] = $video['thumb2'];
            $video['video_path'] = $teleplay['video_path'];

        }

        if($uid){
            $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
            $uid=$Users->where('user_login='.$useraname)->getField('id');

            $video['user_avatar'] = $Users->where('id='.$uid)->getField('avatar');
            //是否点赞
            $map_up['uid'] = $uid;
            $map_up['video_id'] = $video['id'];
            $is_thumbsup = $Thumbsup->where($map_up)->find();


            //是否收藏
            $map_col['uid'] =$uid;
            $map_col['vid'] = $video['id'];
            $is_collect = $CollectVideo->where($map_col)->find();


            if($video['uid'] == $uid){
                $is_purchase = 1;
            } else {
                    //是否购买
                    $map_pur['uid'] = $uid;
                    $map_pur['vid'] = $video['id'];
                    $map_pur['status'] = 1;
                    $map_pur['type'] = 1;
                    $is_purchase = $Purchase->where($map_pur)->find();

            }

            //是否下载
            if($video['uid'] == $uid){
                $is_download = 1;
            }else {
                $map_down['uid'] = $uid;
                $map_down['vid'] = $video['id'];
                $map_down['status'] = 1;
                $map_down['type'] = 2;
                $is_download = $Purchase->where($map_down)->find();
            }
        }

        //是否点赞
        $is_thumbsup?$is_thumbsup=1:$is_thumbsup=0;
        $video['is_thumbsup'] =$is_thumbsup;

        //是否收藏
        $is_collect?$is_collect=1:$is_collect=0;
        $video['is_collect'] =$is_collect;

        //是否购买
        $is_purchase?$is_purchase=1:$is_purchase=0;
        $video['is_purchase'] =$is_purchase;

        //是否下载
        $is_download?$is_download=1:$is_download=0;
        $video['is_download'] =$is_download;


        //评论列表
        $map_com['video_id'] =$id;
        $comment = $Comment->where($map_com)->order('uptime desc')->select();
        foreach ($comment as &$com){
            $com['uptime'] = transfer_time($com['uptime']);
            $com['nickname'] = $Users->where('id='.$com['uid'])->getField('user_nicename');
            $com['avatar']  = $Users->where('id='.$com['uid'])->getField('avatar');
        }
        $video['comment'] =$comment;

        //推荐视频
      /*  if($uid){
            $Experlevel=M("experlevel",'cmf_');
            $experience=$Users->where("id='$uid'")->getField("consumption");
            $level=$Experlevel->where("level_up>='$experience'")->order("levelid asc")->find();
            if($level['levelid']){
                $map_rec['rec_level'] = $level['levelid'];
                $rec_video = $Video->where($map_rec)->order('uptime desc')->limit(2)->select();
                $video['rec_video'] = $rec_video;
            }
        }*/

        response(1, $video);
    }

    /**
     * 收藏视频
     */
    public function collectVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Collect=M('collect_video','cmf_');
        $Users = M('users','cmf_');

        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $id= I('id');
        $where['uid'] = $uid;
        $where['vid'] = $id;
        $is_exists= $Collect->where($where)->find();
        if($is_exists){
            $result= $Collect->where($where)->delete();
            if($result) {
                response(1, null, '取消收藏');
            }else{
                response(-1, null, '取消收藏失败');
            }
        }else{
            $map['uid'] = $uid;
            $map['vid'] = $id;
            $map['uptime'] = time();
            $result = $Collect->add($map);
            if($result){
                response(1, null, '收藏成功');
            }else{
                response(-1, null, '收藏失败');
            }
        }
    }

    /**
     * 收藏列表
     */
    public function collectList(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Collect=M('collect_video','cmf_');
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');

        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');
        $where['uid'] =$uid;
        $list =$Collect->where($where)->order('uptime desc')->select();
        foreach ($list as &$li){
            $li['video_name'] =$Video->where('id='.$li['vid'])->getField('name');
            $li['thumb2'] =$Video->where('id='.$li['vid'])->getField('thumb2');
            $li['video_type'] =$Video->where('id='.$li['vid'])->getField('video_type');
            $li['uptime'] = date('Y-m-d H:i:s', $li['uptime']);
        }

        response(1,$list);
    }

    /**
     *删除收藏
     */
    public function delcollect(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Collect=M('collect_video','cmf_');
        $Users = M('users','cmf_');

        $id=I('id');
        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');
        $where['uid'] =$uid;
        $where['id'] = array('in',$id);

        $result= $Collect->where($where)->delete();
        if($result){
            response(1,null,'删除收藏成功');
        }else{
            response(-1,null,'删除收藏失败');
        }

    }


    /**
     * 点赞视频
     */
    public function thumbs_up(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Thumbs_up=M('thumbs_up','cmf_');
        $Users = M('users','cmf_');

        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $id= I('id');
        $where['uid'] = $uid;
        $where['video_id'] = $id;
        $is_exists= $Thumbs_up->where($where)->find();
        if($is_exists){
            $result= $Thumbs_up->where($where)->delete();
            if($result) {
                $count=$Thumbs_up->where('video_id='.$id)->count();
                $data['thumbs'] =$count;
                response(1, $data, '取消点赞');
            }else{
                response(-1, null, '取消点赞失败');
            }
        }else{
            $map['uid'] = $uid;
            $map['video_id'] = $id;
            $map['uptime'] = time();
            $result = $Thumbs_up->add($map);
            if($result){
                $count=$Thumbs_up->where('video_id='.$id)->count();
                $data['thumbs'] =$count;
                response(1, $data, '点赞成功');
            }else{
                response(-1, null, '点赞失败');
            }
        }
    }
    /**
     * 评论视频
     */
    public function  commentVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Users = M('users','cmf_');
        $Comment = M('comment','cmf_');
        $uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $id= I('id');
        $content = I('content');
        $data['video_id'] =$id;
        $data['content'] = $content;
        $data['uid'] = $uid;
        $data['uptime'] = time();
        $result = $Comment->add($data);
        $where['video_id']=$id;
        $where['uid'] =$uid;
        $res = $Comment->where($where)->order('uptime desc')->select();
        foreach ($res as &$com){
            $com['uptime'] = transfer_time($com['uptime']);
            $com['nickname'] = $Users->where('id='.$com['uid'])->getField('user_nicename');
            $com['avatar']  = $Users->where('id='.$com['uid'])->getField('avatar');
        }
        if($result){
            response(1,$res, '评论成功');
        }else{
            response(-1, $res, '评论失败');
        }
    }

    /**
     * 购买视频/下载视频
     */
    public function purchaseVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Purcase = M('purchase','cmf_');
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');
        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();

        $m_uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$m_uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');
        $jishu = I('jishu');
        $vid= I('vid');
        $paytype= I('paytype');
        //$money = I('money');
        $type = I('type'); //1购买2下载3推荐
        if($type == 1) { //购买视频
            $coin = $Video->where('id=' . $vid)->getField('charge_money');
            $record_type = 11;
        }else if($type == 2){ //下载购买
            $coin = $Video->where('id=' . $vid)->getField('down_money');
            $record_type = 12;
        }

        $video =$Video->where('id=' . $vid)->find();
        $v_uid=$video['uid'];
        $money = $coin/$config['cash_rate'];

        if($jishu){
            $data['jishu'] = $jishu;
        }
        $data['uid'] = $uid;
        $data['vid'] = $vid;
        $order_no = date('Ymd'). str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $data['order_no'] = $order_no;
        $data['coin'] = $coin;
        $data['uptime'] = time();
        $data['paytype'] =$paytype;
        $data['type'] = $type;
        $pur_type=1;
        if($paytype == 1 ){ //支付宝支付
            $data['money'] = $money;
            $data['status'] = 0;
        }else if($paytype ==2 ){ //微信支付
            $data['money'] = $money;
            $data['status'] = 0;
        }else if($paytype ==3 ){ //余额支付
            $my_money = M('member')->where('uid='.API_UID)->getField('amount');
            if($money > $my_money){
                response(-1,null,'您的余额不足,请充值');
            }
            //扣除购买视频者的余额
            M('member')->where('uid='.$m_uid)->setDec('amount',$money);

            //系统抽成，给上传者
            $watch_ratio = $coin*$config['watch_ratio']/100;
            $coin2= $coin-$watch_ratio;

            //给上传者播豆
            $Users->where('id='.$v_uid)->setInc('votes',$coin2);
            //上传者消费记录
            payrecord($v_uid,0,$coin2,14,$pur_type);

            $data['money'] = $money;
            $data['status'] = 1;
        }else if($paytype ==4 ){ //播豆支付
            $my_coin = $Users->where('id='.$uid)->getField('coin');
            if($coin >$my_coin){
                response(-2,null,'您的钻石不足,请充值');
            }

            //扣除观看者的播豆
            $Users->where('id='.$uid)->setDec('coin',$coin);

            $money= $coin;
            $data['money'] = $coin;
            $data['status'] = 1;
            $pur_type=2;

            //系统抽成
            if($type ==1) {
                $ratio = $coin * $config['watch_ratio'] / 100;
            }else if ($type == 2){
                $ratio = $coin * $config['download_ratio'] / 100;
            }
            $coin2= $coin-$ratio;
            //给上传者播豆
            $Users->where('id='.$uid)->setInc('votes',$coin2);
            //上传者消费记录
            payrecord($video['uid'],0,$coin2,14,$pur_type);
        }

        $result = $Purcase->add($data);

        if($result){
            if($paytype ==4){
                //消费记录
                payrecord($m_uid,$result,$money,$record_type,$pur_type);
            }
            $res['order_no'] =$order_no;
            response(1,$res,'支付成功');
        }else{
            response(-1,null,'支付失败');
        }

    }

    /**
     * 推荐等级
     */
    public function  rec_level(){
        $level = M('experlevel','cmf_');
        $list = $level->field('levelid,levelname,recommend_ratio')->order('levelid asc')->select();
        response(1,$list);
    }

    /**
     *  推荐视频,购买
     */
    public function downloadVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Download = M('recommend_video','cmf_');
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');
        $Experlevel=M('experlevel','cmf_');
        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();

        $m_uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$m_uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $vid= I('vid');
        $paytype= I('paytype');
        $money = I('money');
        $level  = I('level');

        $video = $Video->where('id='.$vid)->find();

        $order_no = date('Ymd'). str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        //$coin = $Video->where('id='.$vid)->getField('down_money');
        $money=$Experlevel->where('levelid='.$level)->getField('recommend_ratio');
        //$money = $coin*$config['cash_rate']/100;

        $data['uid'] = $uid;
        $data['vid'] = $vid;
        $data['order_no'] = $order_no;
        $data['coin']    = $money;
        $data['uptime']  = time();
        $data['paytype'] = $paytype;
        $data['level'] = $level;
        $pur_type=1;

        if($paytype == 1 ){ //支付宝支付
            $data['money'] = $money;
            $data['status'] = 0;
        }else if($paytype ==2 ){ //微信支付
            $data['money'] = $money;
            $data['status'] = 0;
        }else if($paytype ==3 ){ //余额支付
            $my_money = M('member')->where('uid='.API_UID)->getField('amount');

            
            if( ($money) > ($my_money) ){
                response(-1,null,'您的余额不足,请充值');
            }
           
            $data['money'] = $money;
            $data['status'] = 1;

            //扣除购买视频者的余额
            M('member')->where('uid='.$m_uid)->setDec('amount',$money);
            //给上传者播豆
            M('member')->where('uid='.$video['uid'])->setInc('amount',$money);
          //  $Users->where('id='.$video['uid'])->setInc('coin',$coin2);
            //上传者消费记录
            payrecord($m_uid,0,$money,15,$pur_type);

        }

        $result = $Download->add($data);

        if($result){
            //消费记录
           // payrecord($m_uid,$result,$money,12,$pur_type);
            $res['order_no'] =$order_no;
            response(1,$res,'支付成功');
        }else{
            response(-1,null,'支付失败');
        }

    }

    /**
     * 打赏视频
     */
    public  function rewardVideo(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $Reward = M('reward','cmf_');
        $Users = M('users','cmf_');
        $Video = M('video','cmf_');
        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();

        $m_uid= API_UID;
        $useraname = M('ucenter_member')->where('id='.$m_uid)->getField('username');
        $uid=$Users->where('user_login='.$useraname)->getField('id');

        $id=I('vid');
        $money = I('money');
        $paytype = I('paytype');

        $video = $Video->where('id='.$id)->find();

        $data['uid'] = $uid;
        $data['vid'] = $id;
        $data['paytype'] = $paytype;
        $data['uptime'] = time();
        $pur_type = 1;
        if($paytype == 1){ //支付宝
            $data['status'] = 0;
            $data['coin'] = $money*$config['cash_rate'];
            $data['money'] = $money;
        }else if($paytype == 2){ //微信
            $data['status'] = 0;
            $data['coin'] = $money*$config['cash_rate'];
            $data['money'] = $money;

        }else if($paytype == 3){ //播豆
            $coin = $Users->where('id='.$uid)->getField('coin');
            if($money >$coin){
                response(-1,null,'您的播豆不足,请充值');
            }
            $data['money'] = $money;
            $data['coin'] = $money;
            $data['status'] = 1;
            $pur_type=2;
            $data['status'] = 1;
            //扣除观看者的播豆
            $Users->where('id='.$uid)->setDec('coin',$money);

            //系统抽成
            $watch_ratio = $money*$config['reward_ratio']/100;
            $coin2= $money-$watch_ratio;

            //给上传者播豆
            $Users->where('id='.$video['uid'])->setInc('votes',$coin2);
            //上传者消费记录
            payrecord($m_uid,0,$coin2,16,$pur_type);
        }

        $order_no = date('Ymd'). str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $data['order_no'] = $order_no;

        $result = $Reward->add($data);
        if($result){
            //消费记录
            payrecord($m_uid,$result,$money,12,$pur_type);
            $res['order_no'] =$order_no;
            response(1,$res,'支付成功');
        }else{
            response(-1,null,'支付失败');
        }

    }


    /**
     * 上传视频配置
     */
    public  function  videoConfig(){
        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();
        $data['is_upload'] = $config['is_upload'];
        $data['is_download'] = $config['is_download'];
        $data['is_watch']    = $config['is_watch'];
        response(1,$data);
    }


    /**
     * 游戏列表
     */
    public function  gameList(){
        $type = I('type');
        $game=M('game_recommend','cmf_');
        if($type=='top'){
            $limit=6;
        }else{
            $limit=10000;
        }

        $list = $game->order('uptime desc')->limit(0,$limit)->select();
        response(1,$list);
    }

    /**
     * 免责声明
     */
    public  function disclaimer(){
        $info = M('disclaimer','cmf_')->find();
        response(1,$info);
    }
    /**
     * 金额转换
     */
    public  function exchangeMoney(){
        $coin = I('money');
        $Video = M('video','cmf_');
        $Config =  M('config_private','cmf_');

        $config = $Config->where('id=1')->find();
        //$price = $Video->where('id='.$vid)->getField('charge_money');
        $money = $coin*$config['cash_rate']/100;
        $data['money'] = $money;
        response(1, $data);
    }


}